﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace Microsoft.HomeServer.HomeServerConsoleTab.TV4Home.Server.WHS.AddIn
{
    public static class Utils
    {
        public static class Misc
        {
            public static string AddInVersion
            {
                get
                {
                    return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
                }
            }
        }

        public static class ComponentVerifier
        {
            public enum VerificationStatus
            {
                NotInstalled,
                Error,
                Warning,
                Installed
            }

            public class VerificationResult
            {
                public VerificationStatus Status { get; private set; }
                public string VerificationMessage { get; private set; }

                public VerificationResult()
                {
                }

                public VerificationResult(VerificationStatus status)
                {
                    this.Status = status;

                    // set default verification messages
                    switch (status)
                    {
                        case VerificationStatus.NotInstalled:
                            VerificationMessage = "Not Installed";
                            break;
                        case VerificationStatus.Error:
                            VerificationMessage = "Error(s) encountered";
                            break;
                        case VerificationStatus.Warning:
                            VerificationMessage = "Warning(s) encountered";
                            break;
                        case VerificationStatus.Installed:
                            VerificationMessage = "OK";
                            break;
                        default:
                            throw new ArgumentOutOfRangeException("status");
                    }
                }

                public VerificationResult(VerificationStatus status, string message)
                {
                    this.Status = status;
                    this.VerificationMessage = message;
                }
            }

            public static VerificationResult CheckTV4HomeWebInterface()
            {
                if (File.Exists(CommonPaths.WebInterface_WebDotConfigPath))
                    return new VerificationResult(VerificationStatus.Installed);
                else
                    return new VerificationResult(VerificationStatus.NotInstalled);
            }

            public static VerificationResult CheckCustomConfiguration()
            {
                if (!File.Exists(CommonPaths.TVServer_InstallDirectory + @"\SetupTv.config") || !File.Exists(CommonPaths.TVServer_InstallDirectory + @"\TvService.config"))
                    return new VerificationResult(VerificationStatus.NotInstalled);
                else
                    return new VerificationResult(VerificationStatus.Installed);
            }

            public static VerificationResult CheckTVServer()
            {
                if (File.Exists(CommonPaths.TVServer_SetupTvEXEPath))
                    return new VerificationResult(VerificationStatus.Installed);
                else
                    return new VerificationResult(VerificationStatus.NotInstalled);
            }
        }

        public static class Win32
        {
            #region Constants
            public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
            public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
            public static readonly IntPtr HWND_TOP = new IntPtr(0);
            public static readonly IntPtr HWND_BOTTOM = new IntPtr(1);

            #region SetWindowPos Constants
            public const UInt32 SWP_NOSIZE = 0x0001;
            public const UInt32 SWP_NOMOVE = 0x0002;
            public const UInt32 SWP_NOZORDER = 0x0004;
            public const UInt32 SWP_NOREDRAW = 0x0008;
            public const UInt32 SWP_NOACTIVATE = 0x0010;
            public const UInt32 SWP_FRAMECHANGED = 0x0020;  /* The frame changed: send WM_NCCALCSIZE */
            public const UInt32 SWP_SHOWWINDOW = 0x0040;
            public const UInt32 SWP_HIDEWINDOW = 0x0080;
            public const UInt32 SWP_NOCOPYBITS = 0x0100;
            public const UInt32 SWP_NOOWNERZORDER = 0x0200;  /* Don't do owner Z ordering */
            public const UInt32 SWP_NOSENDCHANGING = 0x0400;  /* Don't send WM_WINDOWPOSCHANGING */
            #endregion

            #region GetWindowLong Constants
            public const int GWL_STYLE = (-16);
            #endregion

            #region Window Style Constants
            public const uint WS_OVERLAPPED = 0x00000000;
            public const uint WS_POPUP = 0x80000000;
            public const uint WS_CHILD = 0x40000000;
            public const uint WS_MINIMIZE = 0x20000000;
            public const uint WS_VISIBLE = 0x10000000;
            public const uint WS_DISABLED = 0x08000000;
            public const uint WS_CLIPSIBLINGS = 0x04000000;
            public const uint WS_CLIPCHILDREN = 0x02000000;
            public const uint WS_MAXIMIZE = 0x01000000;
            public const uint WS_CAPTION = 0x00C00000;     /* WS_BORDER | WS_DLGFRAME  */
            public const uint WS_BORDER = 0x00800000;
            public const uint WS_DLGFRAME = 0x00400000;
            public const uint WS_VSCROLL = 0x00200000;
            public const uint WS_HSCROLL = 0x00100000;
            public const uint WS_SYSMENU = 0x00080000;
            public const uint WS_THICKFRAME = 0x00040000;
            public const uint WS_GROUP = 0x00020000;
            public const uint WS_TABSTOP = 0x00010000;
            public const uint WS_MINIMIZEBOX = 0x00020000;
            public const uint WS_MAXIMIZEBOX = 0x00010000;
            #endregion

            #region ShowWindow Constants
            public const int SW_HIDE = 0x0000;
            public const int SW_MAXIMIZE = 0x0003;
            public const int SW_MINIMIZE = 0x0006;
            public const int SW_RESTORE = 0x0009;
            public const int SW_SHOW = 0x0005;
            public const int SW_SHOWDEFAULT = 0x00010;
            public const int SW_SHOWMAXIMIZED = 0x0003;
            public const int SW_SHOWMINIMIZED = 0x0002;
            public const int SW_SHOWMINNOACTIVE = 0x0007;
            public const int SW_SHOWNA = 0x0008;
            public const int SW_SHOWNOACTIVATE = 0x0004;
            public const int SW_SHOWNORMAL = 0x0001;
            #endregion
            #endregion

            #region P/Invoke
            [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

            [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

            [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern int SetWindowPos(IntPtr hwnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, UInt32 wFlags);

            [DllImport("user32.dll", SetLastError = true)]
            public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

            [DllImport("user32.dll")]
            public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool SetForegroundWindow(IntPtr hWnd);
            #endregion
        }

        public static class CommonPaths
        {
            public static string TeamMediaPortal_InstallDirectory
            {
                get { return Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\Team MediaPortal"; }
            }

            public static string TVServer_InstallDirectory
            {
                get { return TeamMediaPortal_InstallDirectory + @"\MediaPortal TV Server"; }
            }

            public static string TVServer_SetupTvEXEPath
            {
                get { return TVServer_InstallDirectory + @"\SetupTv.exe"; }
            }

            public static string WebInterface_WebDotConfigPath
            {
                get { return @"C:\Inetpub\TV4HomeWebInterface\Web.config"; }
            }

            public static string WebInterface_MSIPath
            {
                get { return System.Windows.Forms.Application.StartupPath + @"\TV4Home\TV4Home.WebInterface.Setup.WHS.msi"; }
            }
        }
    }
}
